package cn.agiledata.bank.myspace.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.account.service.AccountSummary;
import cn.agiledata.bank.account.service.IAccountService;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ConvertUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.facade.ServiceFacade;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.myspace.exception.MySpaceException;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class RegAction extends IPBBaseAction {
    //email绑定
    public ActionForward regMail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String email = request.getParameter("email");
        String paypass = request.getParameter("paypass");
        String verifycode = request.getParameter("verifycode");
        String snap = (String) request.getSession().getAttribute("sys_sess_vericode");
        if (!verifycode.equals(snap)) {
            //request.getSession().removeAttribute("sys_sess_vericode");
            throw MySpaceException.WRONG_VERIFYCODE;
        }
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        Map map = new HashMap();
        String urlPath = ConvertUtil.getRequestURL(request);
        map.put("email", email);
        map.put("paypass", paypass);
        map.put("urlPath", urlPath);
        map.put("accountId", new Long(accountId));
        IAccountService das = (IAccountService)IPBAppFactory.getObj("accountService");
        das.sendActivateMailAfterRegister(map);
        AccountSummary user = (AccountSummary) request.getSession().getAttribute(Constant.SESSION_ACCOUNT_SUMMARY);
        user.getAccount().setEmail(email);
        request.getSession().setAttribute(Constant.SESSION_ACCOUNT_SUMMARY, user);
        return mapping.findForward("ok");
    }
    //email激活
    public ActionForward mailActive(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String email = request.getParameter("email");
        String activecode = request.getParameter("activecode");
        IAccountService das = (IAccountService)IPBAppFactory.getObj("accountService");
        das.userActivate(email, activecode);
        request.setAttribute("em", email);
        return mapping.findForward("ok");
    }
    //手机绑定
    public ActionForward regMobile(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String mobile = request.getParameter("mobile");
        String paypass = request.getParameter("paypass");
        String vericode = request.getParameter("vericode");
        String snap = (String) request.getSession().getAttribute("sys_sess_vericode");
        if (!vericode.equals(snap)) {
            request.getSession().removeAttribute("sys_sess_vericode");
            throw MySpaceException.WRONG_VERIFYCODE;
        }
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        Map map=new HashMap();
        map.put("accountId", new Long(accountId));
        map.put("register_mobile", mobile);
        map.put("paypass", paypass);
        sf.smsVerify(map);
        request.setAttribute("mob", mobile);
        //edited by wubo 080214
        WeatherUtil.getLoingInfoFromSession(request).getAccount().setMobile(mobile);
        //edit end
        return mapping.findForward("ok");
    }
    //手机激活
    public ActionForward activeMobile(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String mobile = request.getParameter("mobile");
        String mobcode = request.getParameter("mobcode");
        Map map=new HashMap();
        map.put("mobile", mobile);
        map.put("mobcode", mobcode);
        ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        sf.assignMobile(map);
        request.setAttribute("mob", mobile);
        return mapping.findForward("ok");
    }
    //手机激活验证码重发
    public ActionForward reSendToActive(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String vericode = request.getParameter("vericode");
        String snap = (String) request.getSession().getAttribute("sys_sess_vericode");
        if (!vericode.equals(snap)) {
            request.getSession().removeAttribute("sys_sess_vericode");
            throw MySpaceException.WRONG_VERIFYCODE;
        }
        ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        sf.reSendToActive(accountId);
        request.setAttribute("reSendMsg", "ok");
        return mapping.findForward("ok");
    }
    //发送手机取消码
    public ActionForward sendCancelCode(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String submitToken = "";
        submitToken = (String) request.getSession().getAttribute("submitToken");
        ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        Account ac = sf.getAccountDAO().getAccountById(new Long(accountId));
        String mobile = ac.getMobile();
        String email = ac.getEmail();
        request.setAttribute("mobile", mobile);
        request.setAttribute("email", email);
        sf.sendToCancel(accountId);
        submitToken = "1";
        request.getSession().setAttribute("submitToken", submitToken);
        String emailToken = "0";
        request.getSession().setAttribute("emailToken", emailToken);
        return mapping.findForward("ok");
    }
    //重发送手机取消码
    public ActionForward reSendCancelCode(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String vericode = request.getParameter("vericode");
        String snap = (String) request.getSession().getAttribute("sys_sess_vericode");
        if (!vericode.equals(snap)) {
            request.getSession().removeAttribute("sys_sess_vericode");
            throw MySpaceException.WRONG_VERIFYCODE;
        }
        ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        sf.sendToCancel(accountId);
        Account ac = sf.getAccountDAO().getAccountById(new Long(accountId));
        String mobile = ac.getMobile();
        String email = ac.getEmail();
        request.setAttribute("mobile", mobile);
        request.setAttribute("email", email);
        request.setAttribute("reSendMsg", "ok");
        return mapping.findForward("ok");
    }
    //取消手机绑定
    public ActionForward cancelMobile(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String mobcode = request.getParameter("mobcode");
        IAccountService das = (IAccountService)IPBAppFactory.getObj("accountService");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        Map map=new HashMap();
        map.put("accountId", new Long(accountId));
        map.put("mobcode", mobcode);
        das.userCancelToMob(map);
        return mapping.findForward("ok");
    }
    //发送EMAIL取消手机
    public ActionForward emailCancelMobile(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
    	ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        long id = WeatherUtil.getAccountIdFromSession(request);
        Account ac = sf.getAccountDAO().getAccountById(new Long(id));
        String email = ac.getEmail();
        request.setAttribute("email", email);
    	
        String emailToken = (String) request.getSession().getAttribute("emailToken");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        IAccountService das = (IAccountService)IPBAppFactory.getObj("accountService");
        Map map = new HashMap();
        String urlPath = ConvertUtil.getRequestURL(request);
        map.put("urlPath", urlPath);
        map.put("describe", "手机");
        map.put("accountId", new Long(accountId));
        das.sendCancelMail(map);
        emailToken = "1";
        request.getSession().setAttribute("emailToken", emailToken);
        return mapping.findForward("ok");
    }

    //确认EMAIL取消手机
    public ActionForward confirmEmailCancelMobile(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String activeCode = request.getParameter("activeCode");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        Map map = new HashMap();
        map.put("activeCode", activeCode);
        map.put("accountId", new Long(accountId));
        IAccountService das = (IAccountService)IPBAppFactory.getObj("accountService");
        das.cancelMobile(map);
        return mapping.findForward("ok");
    }
    //发送取消EMAIL
    public ActionForward sendCancelEmail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
    	long accountId = WeatherUtil.getAccountIdFromSession(request);
        String submitToken = (String) request.getSession().getAttribute("submitToken");
        ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        Account ac = sf.getAccountDAO().getAccountById(new Long(accountId));
        String email = ac.getEmail();
        String mobile = ac.getMobile();
        request.setAttribute("email", email);
        request.setAttribute("mobile", mobile);
        IAccountService das = (IAccountService)IPBAppFactory.getObj("accountService");
        Map map = new HashMap();
        String urlPath = ConvertUtil.getRequestURL(request);
        map.put("urlPath", urlPath);
        map.put("describe", "EMAIL");
        map.put("accountId", new Long(accountId));
        das.sendCancelMail(map);
        submitToken = "1";
        request.getSession().setAttribute("submitToken", submitToken);
        String mobileToken = "0";
        request.getSession().setAttribute("mobileToken", mobileToken);
        return mapping.findForward("ok");
    }
    //取消Email绑定
    public ActionForward cancelEmail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String cancelcode = request.getParameter("cancelcode");
        IAccountService das = (IAccountService)IPBAppFactory.getObj("accountService");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        Account ac = sf.getAccountDAO().getAccountById(new Long(accountId));
        request.setAttribute("email", ac.getEmail());
        Map map=new HashMap();
        map.put("accountId", new Long(accountId));
        map.put("cancelcode", cancelcode);
        das.userCancelToEmail(map);
        return mapping.findForward("ok");
    }
    //重发手机码取消EMAIL
    public ActionForward reSendEmailCancelCode(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String vericode = request.getParameter("vericode");
        String snap = (String) request.getSession().getAttribute("sys_sess_vericode");
        if (!vericode.equals(snap)) {
            request.getSession().removeAttribute("sys_sess_vericode");
            throw MySpaceException.VERIFYCODE_INVALID;
        }
        ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        sf.sendToCancel(accountId, true);
        
        String mobileToken = "1";
        request.getSession().setAttribute("mobileToken", mobileToken);
        
        request.getSession().setAttribute("sendMsg", "ok");
        return mapping.findForward("ok");
    }
    //手机取消Email
    public ActionForward mobileCancelEmail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
    	long accountId = WeatherUtil.getAccountIdFromSession(request);
        ServiceFacade sf = (ServiceFacade)IPBAppFactory.getObj("serviceFacade");
        Account ac = sf.getAccountDAO().getAccountById(new Long(accountId));
        String email = ac.getEmail();
        String mobile = ac.getMobile();
        request.getSession().setAttribute("email", email);
        request.getSession().setAttribute("mobile", mobile);
        
        sf.sendToCancel(accountId, true);
        request.getSession().setAttribute("sendMsg", "ok");
        return mapping.findForward("ok");

    }
//  确认手机取消EMAIL
    public ActionForward confirmMobileCancelEmail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String mobcode = request.getParameter("mobcode");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        Map map = new HashMap();
        map.put("mobcode", mobcode);
        map.put("accountId", new Long(accountId));
        IAccountService das = (IAccountService)IPBAppFactory.getObj("accountService");
        das.cancelMail(map);
        return mapping.findForward("ok");
    }
    
    //确认金额
    public ActionForward  sendConfirmAmount(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        return mapping.findForward("ok");
    }
    
    //修改确认金额
    public ActionForward modifyAmount(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IPBException, Exception {
        String mobcode = request.getParameter("amount");
        IAccountService das = (IAccountService)IPBAppFactory.getObj("accountService");
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        Map map=new HashMap();
        map.put("accountId", new Long(accountId));
        map.put("amount", mobcode);
        das.modifyAmount(map);
        return mapping.findForward("myspacecontent");
    }
}
